﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraEditors.Controls;
using QLKDS.DTO;

namespace QLKDS.Forms
{
    public partial class frmContractAdd : XtraForm
    {
        #region Properties
        private ContractManager contractManager;
        private EmployeeManager employeeManager = new EmployeeManager();
        private frmContract Form;
        #endregion

        #region Methods


        /// <summary>
        /// Clear all Textboxes on this form
        /// </summary>
        private void ClearTextBoxes()
        {
            //this.txtCode.Text = String.Empty;
            //this.txtName.Text = String.Empty;
            //this.txtDiscountRetail.Text = String.Empty;
            //this.txtSchool.Text = String.Empty;
            //this.txtDescription.Text = String.Empty;
            //this.dteCreatedDate.DateTime = DateTime.Now;
            //this.dteCreatedDate.Properties.MaxValue = DateTime.Now;
            //this.txtName.Focus();
        }
        #endregion

        #region Init form
        public frmContractAdd(frmContract form)
        {
            InitializeComponent();
            this.contractManager = new ContractManager();
            this.Load_info();
            this.Form = form;
           

        }

        public void Load_info()
        {

            this.lueEmployee.Properties.DataSource = employeeManager.ListEmployee();
            this.lueEmployee.Properties.DisplayMember = "Fullname";
            this.lueEmployee.Properties.ValueMember = "Id";

            this.dteDEnd.DateTime = DateTime.Now;
        }

    




        private void frmContractAdd_Load(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
            this.dteDEnd.DateTime = DateTime.Now;
            this.dteDStart.DateTime = DateTime.Now;
        }
        #endregion

        #region Validate

     
        private void lueEmployee_EditValueChanged(object sender, EventArgs e)
        {
            if (this.lueEmployee.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepEmployee.ClearErrors();
            }
        }

        private void txtCode_TextChanged(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepCode.ClearErrors();
            }
        }

        private void txtName_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepName.ClearErrors();
            }
        }

        private void txtBasicSalary_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtBasicSalary.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepBasicSalary.ClearErrors();
            }
        }



        #endregion

        #region Button_click
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
        }
       
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
                this.dxepCode.SetError(this.txtCode, "Mã hợp đồng không được bỏ trống");
                return;
            }
            else if (this.lueEmployee.Text.Trim() == String.Empty)
            {
                this.dxepEmployee.SetError(this.lueEmployee, "Chọn nhân viên");
                return;
            }
            else if (this.txtName.Text.Trim() == String.Empty)
            {
                this.dxepName.SetError(this.txtName, "Tên hợp đồng không được bỏ trống");
                    return;
            }
            else if(this.txtBasicSalary.Text.Trim()==String.Empty)
            {
                this.dxepBasicSalary.SetError(this.txtBasicSalary,"Lương cơ bản không được bỏ trống");
                return;
            }
            else
            {
                this.dxepCode.ClearErrors();
                this.dxepName.ClearErrors();
                this.dxepEmployee.ClearErrors();
               
                this.dxepBasicSalary.ClearErrors();
            }


            /*****************************************************************/
            // Save employeeManager into database                   
            var contract = new Contract();

            contract.Code = this.txtCode.Text.Trim();
            contract.Name = this.txtName.Text.Trim();
            contract.DStart = this.dteDStart.DateTime;
            contract.DEnd = this.dteDEnd.DateTime;
            contract.EmployeeId = (int)this.lueEmployee.EditValue;
            contract.BasicSalary = float.Parse(this.txtBasicSalary.Text.Trim());


            this.contractManager.CreateContract(contract);
            // save employeeManager

         
            if ((DialogResult)XtraMessageBox.Show("Dữ liệu thêm thành công.Tiếp tục thêm ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ClearTextBoxes();
            }
            else
            {
                this.Close();
            }
            this.Form.PopulateContractGridView();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

   



    





      

 

      

     
        


    }
}
